package com.ge.pp;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;

public class SharpenFilter extends ImageFilter {

	public SharpenFilter(PicturePane p) {
		super(p);
		name = "Sharpen Filter";
		
	}

	public BufferedImage filter(BufferedImage img) throws Exception {
		
		try
		{BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
		Raster inRaster = img.getRaster();		
		WritableRaster outr = newImg.getRaster();		
		
		float[] tl = new float[3];
		float[] t = new float[3];
		float[] tr = new float[3];
		float[] l = new float[3];
		float[] c = new float[3];
		float[] r = new float[3];
		float[] bl = new float[3];
		float[] b = new float[3];
		float[] br = new float[3];
				
		float[] avg = new float[3];
		
		for(int x=1; x<outr.getWidth()-1; x++) {
			for (int y=1; y<outr.getHeight()-1; y++) {
				inRaster.getPixel(x-1, y-1, tl);
				inRaster.getPixel(x, y-1, t);
				inRaster.getPixel(x+1, y-1, tr);
				
				inRaster.getPixel(x-1, y, l);
				inRaster.getPixel(x, y, c);
				inRaster.getPixel(x+1, y, r);
				
				inRaster.getPixel(x-1, y+1, bl);
				inRaster.getPixel(x, y+1, b);
				inRaster.getPixel(x+1, y+1, br);
				
				avg[0] = Math.round((-tl[0]-t[0]-tr[0]-l[0]-r[0]-bl[0]-b[0]-br[0])/8.0f + 2.0f*c[0] );

				avg[1] = Math.round((-tl[1]-t[1]-tr[1]-l[1]-r[1]-bl[1]-b[1]-br[1])/8.0f + 2.0f*c[1]) ;
	
				avg[2] = Math.round((-tl[2]-t[2]-tr[2]-l[2]-r[2]-bl[2]-b[2]-br[2])/8.0f + 2.0f*c[2] ) ;

				
				/*avg[0] = (-tl[0]+-t[0]+-tr[0] +
						-l[0]+8.0f*c[0]+-r[0] +
						-bl[0]+-b[0]+-br[0]) ;
				
				avg[1] = (-tl[1]+-t[1]+-tr[1] +
						-l[1]+8.0f*c[1]+-r[1] +
						-bl[1]+-b[1]+-br[1]) ;
				
				avg[2] = (-tl[2]+-t[2]+-tr[2] +
						-l[2]+8.0f*c[2]+-r[2] +
						-bl[2]+-b[2]+-br[2]) ;*/
				
				outr.setPixel(x,y, avg);
			}
		}
		
		return newImg;
		}
		catch(Exception e)
		{
			throw e;
		}
	}
}
